//加载联动下拉菜单
$.ajaxSetup({
    async: false
});

var json;

$.get("http://139.199.200.112/dtpr/index.php?exam-master-basic-sectionmanager",function (data) {
    json=JSON.parse(data);
});

var data=json.data;

addOption("#subject",data);
//绑定联动事件
$("#subject").change(function () {
    var index=$("#subject").get(0).selectedIndex;
    if(index==0){
        return;
    }
    clear("#volume","请选择册次");
    clear("#section","请选择章节");
    if(data[index-1].nodes===undefined){
        return;
    }
    var volume=data[index-1].nodes;
    addOption("#volume",volume);
    $("#volume").change(function () {
        var Vindex=$("#volume").get(0).selectedIndex;
        if(Vindex==0){
            return;
        }
        clear("#section","请选择章节");
        if(volume[Vindex-1].nodes===undefined){
            return;
        }
        var section=volume[Vindex-1].nodes;
        addOption("#section",section);
    })
});
//给某个表单增加选项
function addOption(selectId,node) {
    var subject=$(selectId)[0];
    for(var i=0;i<node.length;i++){
        var option=document.createElement("option");
        option.value=node[i].id;
        option.text=node[i].text;
        subject.add(option);
    }
}
//清除某个表单的选项
function clear(selectId,message) {
    $(selectId).empty();
    var option=document.createElement("option");
    option.text=message;
    option.value="0";
    $(selectId)[0].add(option);
    //一旦触发选项清楚，立刻清楚所选择的章节以免出错
    selectSection(0,0);
}

//根据网页的显示跳转来确定题目类型
switch (parseInt(getQueryString("id"))){
    case 1:
    case 2:
    case 3: $("input[name='args[questiontype]']").val(getQueryString("id"));break;
    default:$("input[name='args[questiontype]']").val(1);
}

//给隐藏input选择某个章节
function selectSection(id, name) {
    $("#section-id").val(id);
    $("#section-name").val(name);
}

//当第三层章节被选择的时候触发隐藏表单，对隐藏表单进行修改
$("#section").change(function () {
    var id=$("#section").val();
    var name=$("#section").find("option:selected").text();
    selectSection(id,name);
});